﻿/*
 * Copyright (c) 2009, 北京理工大学  王柳茗
 * All rights reserved.
 *
 * $Revision$
 *
 * $Author$
 *
 * $Date$
 *
 * $Log$
 *
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace wlm.wsx
{
    /// <summary>
    /// 
    /// </summary>
    public class Scanner
    {
        public Scanner()
        {
        }

        public Scanner(string str)
        {
            string buff = string.Empty;
            if (File.Exists(str))
                _Reader = new XmlTextReader(str);
            else
                _Reader = new XmlTextReader(new StringReader(str));

            _Reader.WhitespaceHandling = WhitespaceHandling.None;
        }
        /// <summary>
        /// 
        /// </summary>
        public int Token
        {
            get
            {
                return _Token;
            }
            set
            {
                _Token = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public int Operator1
        {
            get
            {
                return _Operator1;
            }
            set
            {
                _Operator1 = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public int Operator2
        {
            get
            {
                return _Operator2;
            }
            set
            {
                _Operator2 = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void NextToken()
        {
            try
            {
                string name = string.Empty;
                while (_Reader.Read())
                {
                    switch (_Reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (_Reader.Name.ToLower() == "operator")
                            {
                                if (_Reader.GetAttribute("type") == "+")
                                {
                                    _Reader.Read();
                                    _Reader.Read();
                                    if (_Reader.NodeType == XmlNodeType.Text)
                                        Operator1 = int.Parse(_Reader.Value);

                                    _Reader.Read();
                                    _Reader.Read();
                                    _Reader.Read();
                                    if (_Reader.NodeType == XmlNodeType.Text)
                                        Operator2 = int.Parse(_Reader.Value);

                                    _Token = Tokens.PLUS;
                                    _Reader.Read();
                                    _Reader.Read();
                                }
                            }
                            return;
                    }
                }
            }
            catch (Exception) { }
            _Token = Tokens.EOF;
        }
        private int _Token = 0;
        private int _Operator1 = 0;
        private int _Operator2 = 0;
        private XmlTextReader _Reader;
    }
}
